
// check phone number
export function yhchenckPhone(options) {
    const sMobile = options.phone;

    if (!(/^1[0-9][0-9]\d{8}$/.test(sMobile))) {
        return false;
    } else {
        return true;
    }
}
// 
export function yhdelay(options) {
  return setTimeout(options.func, options.time);
}

// 日期补零
export function yhcheckDateFormatter(dateNum) {
    return (dateNum > 9 ? dateNum : "0" + dateNum);
}
// 获得日期
export function yhgetDateStr(date, addOne) {
    const monthonFlag = (addOne === 1) ? 0 : 1;
    return date.getFullYear() + "-" + this.checkDateFormatter(date.getMonth() + monthonFlag) + "-" + this.checkDateFormatter(date.getDate()) + " " + this.checkDateFormatter(date.getHours()) + ":" + this.checkDateFormatter(date.getMinutes());
}
// 获得日期 **年 **月 **日
export function yhgetDateStrHZ(date, addOne) {
    const monthonFlag = (addOne === 1) ? 0 : 1;
    return date.getFullYear() + "-" + this.checkDateFormatter(date.getMonth() + monthonFlag) + "-" + this.checkDateFormatter(date.getDate()) + " " + this.checkDateFormatter(date.getHours()) + ":" + this.checkDateFormatter(date.getMinutes());
}


// 
export function yhreturnFloatNum(num) {
    const strArr = (typeof (num) + "").toUpperCase() === "string" ? num.split(".") : num.toString().split(".");
    const strVal = strArr.pop();
    return strVal.length;
}

export function yhhasEmojiCharacter(substring) {
    for (var i = 0; i < substring.length; i++) {
        const hs = substring.charCodeAt(i);
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const ls = substring.charCodeAt(i + 1);
                const uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    return true;
                }
            }
        } else if (substring.length > 1) {
            const ls = substring.charCodeAt(i + 1);
            if (ls === 0x20e3) {
                return true;
            }
        } else {
            if (0x2100 <= hs && hs <= 0x27ff) {
                return true;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                return true;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                return true;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                return true;
            } else if (hs === 0xa9 || hs === 0xae || hs === 0x303d || hs === 0x3030 ||
                hs === 0x2b55 || hs === 0x2b1c || hs === 0x2b1b ||
                hs === 0x2b50) {
                return true;
            } else {
                return false;
            }
        }
    }
}

export function yhcompareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
        v1.push('0')
    }
    while (v2.length < len) {
        v2.push('0')
    }

    for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i])
        const num2 = parseInt(v2[i])

        if (num1 > num2) {
            return 1
        } else if (num1 < num2) {
            return -1
        }
    }

    return 0
}


export function yhfindObjFromArrByProperty(arrary, property, propertyBol) {
    if (propertyBol) {
        let key;
        for (var i in property) {
            key = i;
        }

        for (var i = 0; i < arrary.length; i++) {
            const item = arrary[i];
            if (item[key] === property[key]) {
                return item;
            }
        }
    } else {
        for (var i = 0; i < arrary.length; i++) {
            const item = arrary[i];
            if (item === property) {
                return item;
            }
        }
    }
}


export function yhgetUrlParsByObj(obj) {
    var urPar = '';
    for (var key in obj) {
        var value = obj[key];
        if (typeof (value) === 'object') {
            var v = JSON.stringify(value);
            v = v.replace('&', '%26');
            v = v.replace('=', '%3D');
            urPar = urPar + key + "=" + v + "&"
        } else {
            if (isNaN(value)) {
                value = value ? value : "undefined";
                value = value.replace('&', '%26');
                value = value.replace('=', '%3D');
            }
            urPar = urPar + key + "=" + value + "&"
        }
    }
    return urPar.substr(0, urPar.length - 1);
}

export function yhdecodeUrlString(str) {
    if (typeof (str) === 'string') {
        str = str.replace('%26', '&');
        str = str.replace('%3D', '=');
    }
    return str;
}

export function yhcUrlToJson(urlC) {
    const urlComponents = decodeURIComponent(urlC.split("?").pop()).split("&");
    const obj = {};
    urlComponents.forEach(function (item) {
        const itemComponents = item.split("=");
        obj[itemComponents[0]] = itemComponents[1];
    });
    return obj;
}

// 
export function yharraryDelectObjects(options) {
    const index = options.arrary.indexOf(options.startObj);
    options.arrary.splice(index, options.length);
    return options.arrary;
}
// 
export function yharraryGetItemByItemProperty(arrary, prperty) {
    if (!arrary) {
        console.error("has no arrary!");
        return false;
    };
    let keyt, keyIn = 0;
    for (var key in prperty) {
        if (keyIn >= 1) {
            console.error("prperty has too much keys!");
            return false;
        };
        keyt = key;
        keyIn += 1;
    };
    const objvalue = false;
    for (var index in arrary) {
        const obj = arrary[index];
        for (var key in obj) {
            if ((key === keyt) && (obj[key] === prperty[keyt])) {
                return obj;
            };
        };
    };
    return objvalue;
}


// 
export function yhcheckMultipleAction(options) {
    console.log(' = = = = ');
    if (!Object.prototype.hasOwnProperty.call(options, "event")) {
        console.error('YHTOOLS - checkMultipleAction must have event args !');
        return;
    }
    if (!Object.prototype.hasOwnProperty.call(options, "page")) {
        console.error('YHTOOLS - checkMultipleAction must have event page !');
        return;
    }
    const d = Object.prototype.hasOwnProperty.call(options, "duration") ? options['duration'] : 400;
    const idf = 'YH' + options.event;
    const page = options.page;
    const now = (new Date()).getTime();
    if (!Object.prototype.hasOwnProperty.call(page, idf)) {
        page[idf] = now;
        console.log('!page.hasOwnProperty(idf)');
        return false;
    } else {
        const tm = page[idf];
        const bt = now - tm < d;
        page[idf] = now;
        console.log('page.hasOwnProperty(idf)');
        return bt;
    }
}

export function yhobjSetByObj(options) {
    // options = {handleO:handleO,valueO:valueO}
    const handleO = options.handleO;
    var valueO = options.valueO;
    for (var key in handleO) {
        if (Object.prototype.hasOwnProperty.call(valueO, key)) {
            handleO[key] = valueO[key]
        }
    }
    return handleO;
}
// get week day
export function yhgetWeekDayHZ(date) {
    const weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    const mon = weeks[date.getDay()];
    return mon;
}
// ellipsis str
export function yhellipsisStr(parms) {
    if (!Object.prototype.hasOwnProperty.call(parms, 'str')) {
        console.error('-YHTOOLS TIPS : function -ellipsisStr- must has prams named str');
    };
    if (!Object.prototype.hasOwnProperty.call(parms, 'len')) {
        console.error('-YHTOOLS TIPS : function -ellipsisStr- must has prams named len');
    }
    return parms.str.length > parms.len ? parms.str.substr(0, parms.len) + "..." : parms.str;
}
// toRoundfixed
export function yhroundFixed(parms) {
    if (!Object.prototype.hasOwnProperty.call(parms, 'num')) {
        console.error('-YHTOOLS TIPS : function -ellipsisStr- must has prams named str');
    };
    if (!Object.prototype.hasOwnProperty.call(parms, 'count')) {
        console.error('-YHTOOLS TIPS : function -ellipsisStr- must has prams named len');
    }
    if (parms.count === 0) {
        return Math.round(parms.num);
    }
    const m = Math.pow(10, parms.count);
    const n = Math.round(parms.num * m) / m;
    return n.toFixed(parms.count);
}
// hid num str
export function yhhideMiddlePhone(string) {
    return string.substr(0, 3) + "****" + string.substr(3, 4);
}

export default {
    yhchenckPhone,
    yhdelay,
    yhcheckDateFormatter,
    yhgetDateStr,
    yhgetDateStrHZ,
    yhreturnFloatNum,
    yhhasEmojiCharacter,
    yhcompareVersion,
    yhfindObjFromArrByProperty,
    yhgetUrlParsByObj,
    yhdecodeUrlString,
    yhcUrlToJson,
    yharraryDelectObjects,
    yharraryGetItemByItemProperty,
    yhcheckMultipleAction,
    yhobjSetByObj,
    yhgetWeekDayHZ,
    yhellipsisStr,
    yhroundFixed,
    yhhideMiddlePhone,
}
